Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Text

Imports SQLtoASPX.Support

Public Class MasterASPXVB
    Private TableGen As RawCreateScript
    'Private mDatabaseName As String

    Public Sub New(ByVal TableGen As RawCreateScript)
        Me.TableGen = TableGen
    End Sub

    Public Overrides Function ToString() As String
        Dim sb As New StringBuilder

        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005
                If TableGen.IsAddImports = True Then

                    sb.AppendLine("Imports " & Support.GetDatabaseImportLine(TableGen) & "")
                    Select Case TableGen.Paging
                        Case PagingEnum.Basic
                            sb.AppendLine("Imports System.Data")
                    End Select
            
                    sb.AppendLine("")
                End If
        End Select

        Select Case TableGen.CodeType
            Case CodeTypeEnum.CS2005
                sb.AppendLine("using System;")
                sb.AppendLine("using System.Data;")
                sb.AppendLine("using System.Configuration;")
                sb.AppendLine("using System.Collections;")
                sb.AppendLine("using System.Web;")
                sb.AppendLine("using System.Web.Security;")
                sb.AppendLine("using System.Web.UI;")
                sb.AppendLine("using System.Web.UI.WebControls;")
                sb.AppendLine("using System.Web.UI.WebControls.WebParts;")
                sb.AppendLine("using System.Web.UI.HtmlControls;")
                sb.AppendLine("using System.Text.RegularExpressions;")
                sb.AppendLine("using " & Support.GetDatabaseImportLine(TableGen) & ";")
                sb.AppendLine("")
        End Select

        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005
                sb.AppendLine("Partial Class " & TableGen.ClassName & "")
            Case CodeTypeEnum.CS2005
                sb.AppendLine("public partial class " & TableGen.ClassName & " : System.Web.UI.Page")
                sb.AppendLine("{")
        End Select

        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005
                sb.AppendLine("    Inherits System.Web.UI.Page")
                sb.AppendLine("")
        End Select
        Dim spaces As New Space(1)

        sb.Append(GetMasterPageLoad(TableGen))

        GetGetSQL(sb, TableGen, New Space(1))

        Select Case TableGen.EditKeyPassing
            Case EditKeyPassingEnum.Inline
            Case Else
                sb.Append(GetMasterAddNewClick(TableGen))
        End Select

        sb.Append(GetMasterSearchClick(TableGen))

        GetMasterGridOnRowCommand(sb, TableGen, spaces)

        sb.Append(Support.GetSessionValueFunction(TableGen))

        sb.Append(GetPageIndexChanging())

        sb.Append(GetGrdSorting())

        GetProtectedSubgrdRowDataBound(spaces, sb, TableGen)
        grdItemCreatedFunction(spaces, sb, TableGen)
        grdRowCreatedFunction(spaces, sb, TableGen)
        GetPageCountFunction(spaces, sb, TableGen)
        GetProtectedSubcmdFirstClick(spaces, sb, TableGen)
        GetProtectedSubcmdNextClick(spaces, sb, TableGen)
        GetProtectedSubcmdPreviousClick(spaces, sb, TableGen)
        ProtectedSubcmdLastClick(sb, TableGen, spaces)
        GetUpdateLabelsSub(sb, TableGen, spaces)
        cboThemeSelectedIndexChangedFunction(spaces, sb, TableGen)

        Select Case TableGen.EditKeyPassing
            Case EditKeyPassingEnum.Inline
                GetDetailASPXDeleteClick(spaces, sb, TableGen)
            Case Else
        End Select

        LoadSubComboBoxSearchInitializeCodeVB(spaces, sb, TableGen, "Search")
        Debug.WriteLine(TableGen.ClassName)
        If TableGen.ClassName = "WorkOrderCSCodeBehindInlineBasicPaging" Then
            Debug.WriteLine(TableGen.ClassName)
        End If
        Select Case TableGen.EditKeyPassing
            Case EditKeyPassingEnum.Inline

                LoadSubComboBoxInitializeCodeVB(spaces, sb, TableGen, "Dialog")
                GetIsDateFunction(spaces, sb, TableGen)

                GetIsNumericFunction(spaces, sb, TableGen)

            Case Else
        End Select


        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005
                sb.AppendLine("End Class")
            Case CodeTypeEnum.CS2005
                sb.AppendLine("}")
        End Select

        Return sb.ToString
    End Function

    Protected Sub ProtectedSubcmdLastClick(ByVal sb As StringBuilder, ByVal TableGen As RawCreateScript, ByVal spaces As Space)

        Select Case TableGen.Paging
            Case PagingEnum.HighSpeed
                sb.AppendLine("")
                Select Case TableGen.CodeType
                    Case CodeTypeEnum.VB2005
                        sb.AppendLine(spaces.ToString & "Protected Sub cmdLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdLast.Click")
                    Case CodeTypeEnum.CS2005
                        sb.AppendLine(spaces.ToString & "protected void cmdLast_Click(object sender, EventArgs e)")
                        sb.AppendLine(spaces.ToString & "{")
                End Select
                spaces.Inc()
                sb.AppendLine(spaces.ToString & SetSessionField(TableGen.SafeTableName & ".CurrentPage", TableGen) & " = GetPageCount()" & EndStatement(TableGen))
                sb.AppendLine(spaces.ToString & "BindGrid()" & EndStatement(TableGen))
                spaces.Dec()
                GetEndSub(sb, spaces, TableGen)
        End Select
    End Sub

    Protected Sub GetUpdateLabelsSub(ByVal sb As StringBuilder, ByVal TableGen As RawCreateScript, ByVal spaces As Space)
        Select Case TableGen.Paging
            Case PagingEnum.HighSpeed
                sb.AppendLine("")
                Select Case TableGen.CodeType
                    Case CodeTypeEnum.VB2005
                        sb.AppendLine(spaces.ToString & "Protected Sub UpdateLabels()")
                    Case CodeTypeEnum.CS2005
                        sb.AppendLine(spaces.ToString & "protected void UpdateLabels()")
                        sb.AppendLine(spaces.ToString & "{")
                End Select
                spaces.Inc()
                sb.AppendLine(spaces.ToString & "lblRecordsFound.Text = ""Records Found "" " & OperatorConcatenate(TableGen) & " " & ReadSessionField(TableGen.SafeTableName & ".RecordsFound", TableGen) & EndStatement(TableGen))
                sb.AppendLine(spaces.ToString & "lblPageNumber.Text = ""Page "" " & OperatorConcatenate(TableGen) & " " & ReadSessionField(TableGen.SafeTableName & ".CurrentPage", TableGen) & " " & OperatorConcatenate(TableGen) & " "" of "" " & OperatorConcatenate(TableGen) & " GetPageCount()" & EndStatement(TableGen))

                spaces.Dec()
                GetEndSub(sb, spaces, TableGen)
        End Select
    End Sub

    Public Function GetPageIndexChanging() As String
        Dim sb As New StringBuilder


        Select Case TableGen.Paging
            Case PagingEnum.Basic
                Select Case TableGen.ViewType
                    Case ViewTypeEnum.GridView
                        sb.AppendLine()
                        Select Case TableGen.CodeType
                            Case CodeTypeEnum.VB2005
                                sb.AppendLine("    Protected Sub grd_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grd.PageIndexChanging")
                            Case CodeTypeEnum.CS2005
                                sb.AppendLine("    protected void grd_PageIndexChanging(object sender, GridViewPageEventArgs e)")
                                sb.AppendLine("    {")

                        End Select
                        sb.AppendLine("       grd.PageIndex = e.NewPageIndex" & EndStatement(TableGen))
                        sb.AppendLine("       BindGrid()" & EndStatement(TableGen))
                        GetEndSub(sb, New Space(1), TableGen)
                End Select
        End Select

        Return sb.ToString
    End Function

    Public Function GetGrdSorting() As String
        GetGrdSorting = ""
        Select Case TableGen.ViewType
            Case ViewTypeEnum.GridView
                Select Case TableGen.Sorting
                    Case True
                        Dim sb As New StringBuilder
                        sb.AppendLine()
                        Dim spaces As New Space(1)
                        Select Case TableGen.CodeType
                            Case CodeTypeEnum.VB2005
                                sb.AppendLine(spaces.ToString & "Protected Sub grd_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grd.Sorting")
                            Case CodeTypeEnum.CS2005
                                sb.AppendLine(spaces.ToString & "protected void grd_Sorting(object sender, GridViewSortEventArgs e)")
                                sb.AppendLine(spaces.ToString & "{")
                        End Select

                        sb.AppendLine()
                        spaces.Inc()
                        Select Case TableGen.CodeType
                            Case CodeTypeEnum.VB2005
                                sb.AppendLine(spaces.ToString & "Select Case CStr(Session(""" & TableGen.SafeTableName & ".SortDirection""))")
                            Case CodeTypeEnum.CS2005
                                sb.AppendLine(spaces.ToString & "switch(SessionValue(""" & TableGen.SafeTableName & ".SortDirection""))")
                                sb.AppendLine(spaces.ToString & "{")
                        End Select

                        spaces.Inc()


                        sb.AppendLine(spaces.ToString & CaseStatement("ASC", TableGen))

                        spaces.Inc()

                        sb.AppendLine(spaces.ToString & SetSessionField(TableGen.SafeTableName & ".SortDirection", TableGen) & " = ""DESC""" & EndStatement(TableGen))
                        EndCaseSection(sb, spaces, TableGen)
                        spaces.Dec()
                        sb.AppendLine(spaces.ToString & CaseStatement("DESC", TableGen))
                        spaces.Inc()
                        sb.AppendLine(spaces.ToString & SetSessionField(TableGen.SafeTableName & ".SortDirection", TableGen) & " = ""ASC""" & EndStatement(TableGen))
                        EndCaseSection(sb, spaces, TableGen)
                        spaces.Dec()
                        sb.AppendLine(spaces.ToString & CaseElse(TableGen))
                        spaces.Inc()
                        sb.AppendLine(spaces.ToString & SetSessionField(TableGen.SafeTableName & ".SortDirection", TableGen) & " = ""ASC""" & EndStatement(TableGen))
                        EndCaseSection(sb, spaces, TableGen)
                        spaces.Dec()
                        spaces.Dec()
                        sb.AppendLine(spaces.ToString & EndSelect(TableGen))
                        sb.AppendLine()
                        sb.AppendLine(spaces.ToString & SetSessionField(TableGen.SafeTableName & ".SortExpression", TableGen) & " = e.SortExpression" & EndStatement(TableGen))

                        sb.AppendLine()
                        sb.AppendLine(spaces.ToString & "BindGrid()" & EndStatement(TableGen))
                        sb.AppendLine()
                        spaces.Dec()
                        GetEndSub(sb, spaces, TableGen)
                        Return (sb.ToString)
                End Select
        End Select
    End Function

    Public Function GetMasterPageLoad(ByVal TableGen As RawCreateScript) As String

        Dim sb As New StringBuilder

        Dim spaces As New Space(1)

        Select Case TableGen.Paging
            Case PagingEnum.HighSpeed
                Select Case TableGen.CodeType
                    Case CodeTypeEnum.VB2005
                        sb.AppendLine(PrivateVariableAsClassName(TableGen, spaces, "PageSize", "Long", "10"))
                    Case CodeTypeEnum.CS2005
                        sb.AppendLine(PrivateVariableAsClassName(TableGen, spaces, "PageSize", "long", "10"))
                End Select
        End Select


        Support.GetPageLoad(sb, TableGen, spaces)

        spaces.Inc()

        sb.Append(Support.GetIsPostbackEqualFalse(spaces, TableGen))

        spaces.Inc()

        Select Case TableGen.EditKeyPassing
            Case EditKeyPassingEnum.Inline
                LoadClickCallLoadComboBoxes(spaces, sb, TableGen, "Dialog")

            Case Else
                'do nothing
        End Select

        For Each Column As PCGColumn In TableGen.Columns
            If Column.IsDisplayColumn = True Then
                If Column.Search <> "None" Then
                    If Column.LinkTableName.Length > 0 And Column.LinkTableKey.Length > 0 And Column.LinkTableValue.Length > 0 Then

                        Select Case TableGen.CommentLevel
                            Case CommentLevelEnum.Basic
                                AddComment(sb, TableGen, spaces, "Loads elements from the " & Column.LinkTableKey & " table to be searched on")

                            Case CommentLevelEnum.Verbose
                                AddComment(sb, TableGen, spaces, "This function will fill in the search combo box with all the values in the table")

                        End Select
                        sb.AppendLine(spaces.ToString & "Loadcbo" & Column.SafeParameterName("Search") & "()" & EndStatement(TableGen))
                    End If
                End If
            End If
        Next

        Select Case TableGen.EditKeyPassing
            Case EditKeyPassingEnum.Inline
                ' No Need to load session values because we arn't moving pages.
            Case Else
                For Each Column As PCGColumn In TableGen.Columns
                    If Column.Search <> "None" Then
                        If Column.IsDate Then

                            Select Case TableGen.CommentLevel
                                Case CommentLevelEnum.Basic
                                    sb.AppendLine()
                                    AddComment(sb, TableGen, spaces, "Puts the previous state of the " & Column.ControlNameSearch("To") & " field done when the user has searched and moved to the Edit" & TableGen.TableName & " page and then came back")

                                Case CommentLevelEnum.Verbose
                                    sb.AppendLine()
                                    AddComment(sb, TableGen, spaces, "Puts the previous state of the " & Column.ControlNameSearch("To") & " field done when the user has searched and moved to the Edit" & TableGen.TableName & " page and then came back")

                            End Select
                           
                            sb.AppendLine(spaces.ToString & Column.ControlNameSearch("To") & "." & Column.ControlAccessorSearch & " = " & ReadSessionField(TableGen.TableName & "." & Column.ControlNameSearch("To"), TableGen) & EndStatement(TableGen))


                            Select Case TableGen.CommentLevel
                                Case CommentLevelEnum.Basic
                                    AddComment(sb, TableGen, spaces, "Puts the previous state of the " & Column.ControlNameSearch("From") & " field done when the user has searched and moved to the Edit" & TableGen.TableName & " page and then came back")

                                Case CommentLevelEnum.Verbose
                                    AddComment(sb, TableGen, spaces, "Puts the previous state of the " & Column.ControlNameSearch("From") & " field done when the user has searched and moved to the Edit" & TableGen.TableName & " page and then came back")

                            End Select

                            sb.AppendLine(spaces.ToString & Column.ControlNameSearch("From") & "." & Column.ControlAccessorSearch & " = " & ReadSessionField(TableGen.TableName & "." & Column.ControlNameSearch("From"), TableGen) & EndStatement(TableGen))
                        Else

                            Select Case TableGen.CommentLevel
                                Case CommentLevelEnum.Basic
                                    sb.AppendLine()
                                    AddComment(sb, TableGen, spaces, "Puts the previous state of the " & Column.ControlNameSearch & " field done when the user has searched and moved to the Edit" & TableGen.TableName & " page and then came back")

                                Case CommentLevelEnum.Verbose
                                    sb.AppendLine()
                                    AddComment(sb, TableGen, spaces, "Puts the previous state of the " & Column.ControlNameSearch & " field done when the user has searched and moved to the Edit" & TableGen.TableName & " page and then came back")

                            End Select
                         
                            sb.AppendLine(spaces.ToString & Column.ControlNameSearch & "." & Column.ControlAccessorSearch & " = " & ReadSessionField(TableGen.TableName & "." & Column.ControlNameSearch, TableGen) & EndStatement(TableGen))
                        End If
                    End If
                Next
        End Select


        Select Case TableGen.CommentLevel
            Case CommentLevelEnum.Basic

            Case CommentLevelEnum.Verbose
                sb.AppendLine()
                AddComment(sb, TableGen, spaces, "Calls the BindGrid() function")

        End Select
      
        sb.AppendLine(spaces.ToString & "BindGrid()" & EndStatement(TableGen))
        sb.AppendLine(spaces.ToString & "LoadThemes()" & EndStatement(TableGen))
        spaces.Dec()

        GetEndIf(sb, spaces, TableGen, "End If - if IsPostBack equals false")
        '        sb.AppendLine(Spaces.ToString & GetEndIf(TableGen))

        spaces.Dec()


        Select Case TableGen.CommentLevel
            Case CommentLevelEnum.Basic

            Case CommentLevelEnum.Verbose
                sb.AppendLine()
                AddComment(sb, TableGen, spaces, "End Sub - Page_Load")
        End Select
        GetEndSub(sb, spaces, TableGen)


        LoadThemesFunction(spaces, sb, TableGen, True)
        SaveThemesCookieFunction(spaces, sb, TableGen)

        sb.AppendLine()

        Select Case TableGen.CommentLevel
            Case CommentLevelEnum.Basic
                AddComment(sb, TableGen, spaces, "Display's the grid with the search criteria.")

            Case CommentLevelEnum.Verbose
                AddComment(sb, TableGen, spaces, "Display's the grid with the search criteria.")

        End Select
        
        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005
                sb.AppendLine(spaces.ToString & "Private Sub BindGrid()")
            Case CodeTypeEnum.CS2005
                sb.AppendLine(spaces.ToString & "protected void BindGrid()")
                sb.AppendLine(spaces.ToString & "{")
        End Select
        spaces.Inc()
        Select Case TableGen.Paging
            Case PagingEnum.HighSpeed
                sb.AppendLine(spaces.ToString & SetSessionField(TableGen.SafeTableName & ".RecordsFound", TableGen) & " = 0" & EndStatement(TableGen))
        End Select

        Select Case TableGen.Paging
            Case PagingEnum.HighSpeed
                GetIfLengthEqualToZero(ReadSessionField(TableGen.SafeTableName & ".CurrentPage", TableGen), sb, spaces, TableGen)
                spaces.Inc()
                sb.AppendLine(spaces.ToString & SetSessionField(TableGen.SafeTableName & ".CurrentPage", TableGen) & " = 1" & EndStatement(TableGen))
                spaces.Dec()
                GetEndIf(sb, spaces, TableGen)

                'If Len(Session("Classification.SortExpression")) = 0 Then
                '    Session("Classification.SortExpression") = "ClassificationID"
                '    Session("Classification.SortDirection") = "ASC"
                'End If
                Select Case TableGen.Sorting
                    Case True
                        GetIfLengthEqualToZero(ReadSessionField(TableGen.SafeTableName & ".SortExpression", TableGen), sb, spaces, TableGen)
                        spaces.Inc()
                        sb.AppendLine(spaces.ToString & SetSessionField(TableGen.SafeTableName & ".SortExpression", TableGen) & " = ""[" & TableGen.TableName & "]." & TableGen.AutoNumberColumn & """" & EndStatement(TableGen))
                        sb.AppendLine(spaces.ToString & SetSessionField(TableGen.SafeTableName & ".SortDirection", TableGen) & " = ""ASC""" & EndStatement(TableGen))
                        spaces.Dec()
                        GetEndIf(sb, spaces, TableGen)
                End Select
        End Select

        sb.Append(DeclareAndOpenConnection(spaces, TableGen))

        Select Case TableGen.CommentLevel
            Case CommentLevelEnum.Basic

            Case CommentLevelEnum.Verbose
                sb.AppendLine()
                AddComment(sb, TableGen, spaces, "Create a string variable called sql and initialize it to a select statement to display table - " & TableGen.TableName & ".")
        End Select

        sb.AppendLine(DimVariableAsClassName(TableGen, spaces, "sql", "String", "GetSQL()") & EndStatement(TableGen))


        Select Case TableGen.CommentLevel
            Case CommentLevelEnum.Basic

            Case CommentLevelEnum.Verbose
                AddComment(sb, TableGen, spaces, "The command object will be used to execute the sql statement below.")

        End Select

        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005
                sb.AppendLine(spaces.ToString & "Dim Command As New " & GetDatabaseCommandClassName(TableGen) & "()")
            Case CodeTypeEnum.CS2005
                sb.AppendLine(spaces.ToString & GetDatabaseCommandClassName(TableGen) & " Command = new " & GetDatabaseCommandClassName(TableGen) & "();")
        End Select


        Select Case TableGen.CommentLevel
            Case CommentLevelEnum.Basic

            Case CommentLevelEnum.Verbose
                sb.AppendLine()
                AddComment(sb, TableGen, spaces, "Set the connection property on the Command object to the active connection.")

        End Select
        
        sb.AppendLine(spaces.ToString & "Command.Connection = Connection" & EndStatement(TableGen))


        Select Case TableGen.CommentLevel
            Case CommentLevelEnum.Basic

            Case CommentLevelEnum.Verbose
                sb.AppendLine()
                AddComment(sb, TableGen, spaces, "Set the CommandText property on the Command object to the value of the variable sql.")

        End Select

        sb.AppendLine(spaces.ToString & "Command.CommandText = sql" & EndStatement(TableGen))

        For Each Column As PCGColumn In TableGen.Columns
            If Column.Search <> "None" Then
                Select Case TableGen.CodeType
                    Case CodeTypeEnum.VB2005, CodeTypeEnum.CS2005
                        Select Case Column.DataType.ToUpper
                            Case "BIT"
                                ' we use a drop down list, and thus don't use parameters.
                            Case "DATETIME", "SMALLDATETIME", "TIMESTAMP"
                                sb.AppendLine(spaces.ToString & IfGreaterThan(Column.ControlNameSearch("From") & "." & Column.ControlAccessorSearch & ".Length", "0", TableGen))
                                spaces.Inc()

                                sb.AppendLine(spaces.ToString & "Command.Parameters.AddWithValue(""@" & Column.SafeParameterName("From") & """, " & Column.ControlNameSearch("From") & "." & Column.ControlAccessorSearch & ")" & EndStatement(TableGen))

                                spaces.Dec()
                                GetEndIf(sb, spaces, TableGen)

                                sb.AppendLine(spaces.ToString & IfGreaterThan(Column.ControlNameSearch("To") & "." & Column.ControlAccessorSearch & ".Length", "0", TableGen))
                                spaces.Inc()
                                sb.AppendLine(spaces.ToString & "Command.Parameters.AddWithValue(""@" & Column.SafeParameterName("To") & """, " & Column.ControlNameSearch("To") & "." & Column.ControlAccessorSearch & ")" & EndStatement(TableGen))

                                spaces.Dec()
                                GetEndIf(sb, spaces, TableGen)
                            Case Else
                                Select Case Column.Search
                                    Case "Exact"
                                        sb.AppendLine(spaces.ToString & "Command.Parameters.AddWithValue(""@" & Column.SafeParameterName & """, " & Column.ControlNameSearch & "." & Column.ControlAccessorSearch & ")" & EndStatement(TableGen))
                                    Case "Left"

                                        sb.AppendLine(spaces.ToString & "Command.Parameters.AddWithValue(""@" & Column.SafeParameterName & """,""%"" " & OperatorConcatenate(TableGen) & " " & Column.ControlNameSearch & "." & Column.ControlAccessorSearch & ")" & EndStatement(TableGen))


                                    Case "Right"
                                        sb.AppendLine(spaces.ToString & "Command.Parameters.AddWithValue(""@" & Column.SafeParameterName & """," & Column.ControlNameSearch & "." & Column.ControlAccessorSearch & " " & OperatorConcatenate(TableGen) & " ""%"")" & EndStatement(TableGen))
                                    Case "Both"
                                        sb.AppendLine(spaces.ToString & "Command.Parameters.AddWithValue(""@" & Column.SafeParameterName & """,""%"" " & OperatorConcatenate(TableGen) & " " & Column.ControlNameSearch & "." & Column.ControlAccessorSearch & " " & OperatorConcatenate(TableGen) & " ""%"")" & EndStatement(TableGen))
                                    Case Else

                                        sb.AppendLine(spaces.ToString & "Command.Parameters.AddWithValue(""@" & Column.SafeParameterName & """, " & Column.ControlNameSearch & "." & Column.ControlAccessorSearch & ")" & EndStatement(TableGen))
                                End Select

                                '                               GetEndIf(sb, spaces, TableGen)
                                sb.AppendLine(spaces.ToString & "")

                        End Select
                End Select
            End If
        Next

        Select Case TableGen.ViewType
            Case ViewTypeEnum.GridView, ViewTypeEnum.Repeater

                Select Case TableGen.CommentLevel
                    Case CommentLevelEnum.Basic

                    Case CommentLevelEnum.Verbose
                        sb.AppendLine()
                        AddComment(sb, TableGen, spaces, "Set grd.DataSource propery with a DataReader object this is the result of executing the Command object with a fast forward only recordset.")

                End Select
            
                Select Case TableGen.Paging
                    Case PagingEnum.Basic

                        sb.AppendLine(DimVariableAsNewClassName(TableGen, spaces, "DataAdapter", "SqlDataAdapter", "Command"))
                        sb.AppendLine(DimVariableAsNewClassName(TableGen, spaces, "DataSet", "DataSet", ""))
                        sb.AppendLine(spaces.ToString & "DataAdapter.Fill(DataSet)" & EndStatement(TableGen))
                        sb.AppendLine(spaces.ToString & "grd.DataSource = DataSet" & EndStatement(TableGen))
                    Case Else
                        sb.AppendLine(spaces.ToString & "grd.DataSource = Command.ExecuteReader()" & EndStatement(TableGen))
                End Select

                Select Case TableGen.CommentLevel
                    Case CommentLevelEnum.Basic

                    Case CommentLevelEnum.Verbose
                        sb.AppendLine()
                        AddComment(sb, TableGen, spaces, "Calling Databind will run the sql and put the fields in the grid.")

                End Select
                sb.AppendLine(spaces.ToString & "grd.DataBind()" & EndStatement(TableGen))
            Case ViewTypeEnum.CodeBehind


                Select Case TableGen.CommentLevel
                    Case CommentLevelEnum.Basic

                    Case CommentLevelEnum.Verbose
                        sb.AppendLine()
                        AddComment(sb, TableGen, spaces, "Set the grd's CSS Classname to PCGTable.")
                        AddComment(sb, TableGen, spaces, "when grd is rendered it will resolve to a table, and CSSClass will render to Class=""PCGTable""")

                End Select

                sb.AppendLine(spaces.ToString & "grd.CssClass = ""PCG-Content""" & EndStatement(TableGen))

                sb.AppendLine()

                sb.Append(DimVariableAsNewClassName(TableGen, spaces, "TableRow", "TableRow"))

                Dim CellCount As Long = 0
                For Each Column As PCGColumn In TableGen.Columns
                    If Column.DisplayInGrid = True Then
                        If CellCount = 0 Then
                            sb.Append(DimVariableAsNewClassName(TableGen, spaces, "TableCell", "TableCell"))
                        Else
                            sb.AppendLine(spaces.ToString & "TableCell = new TableCell()" & EndStatement(TableGen))
                        End If

                        sb.AppendLine(spaces.ToString & "TableCell.Text = """ & Column.ColumnNameDescriptive & """" & EndStatement(TableGen))

                        sb.AppendLine(spaces.ToString & "TableRow.Cells.Add(TableCell)" & EndStatement(TableGen))


                        CellCount = CellCount + 1
                    End If
                Next Column
                sb.AppendLine(spaces.ToString & "TableCell = new TableCell()" & EndStatement(TableGen))
                sb.AppendLine()

                sb.AppendLine(spaces.ToString & "TableRow.Cells.Add(TableCell)" & EndStatement(TableGen))


                sb.AppendLine(spaces.ToString & "TableRow.CssClass = ""ui-widget-header""" & EndStatement(TableGen))

                sb.AppendLine()

                sb.AppendLine(spaces.ToString & "grd.Rows.Add(TableRow)" & EndStatement(TableGen))


                sb.AppendLine()

                sb.AppendLine(DimVariableAsClassName(TableGen, spaces, "Reader", GetDatabaseReaderClassName(TableGen), "Command.ExecuteReader()") & EndStatement(TableGen))

                DoWhileReaderDotRead(sb, TableGen, spaces)

                spaces.Inc()
                sb.AppendLine()
                sb.AppendLine(spaces.ToString & "TableRow = new TableRow()" & EndStatement(TableGen))

                If TableGen.Paging = PagingEnum.HighSpeed Then
                    GetIfCountEqualToValue("grd.Rows", "1", sb, spaces, TableGen)
                    spaces.Inc()
                    sb.AppendLine(spaces.ToString & SetSessionField(TableGen.SafeTableName & ".RecordsFound", TableGen) & " = " & GenerateReader(TableGen, "RecordSetCount") & EndStatement(TableGen))
                    sb.AppendLine(spaces.ToString & "lblRecordsFound.Text = ""Records Found "" " & OperatorConcatenate(TableGen) & " " & SetSessionField(TableGen.SafeTableName & ".RecordsFound", TableGen) & EndStatement(TableGen))
                    sb.AppendLine(spaces.ToString & "lblPageNumber.Text = ""Page "" " & OperatorConcatenate(TableGen) & " " & SetSessionField(TableGen.SafeTableName & ".CurrentPage", TableGen) & " " & OperatorConcatenate(TableGen) & " "" of "" " & OperatorConcatenate(TableGen) & " GetPageCount()" & EndStatement(TableGen))
                    spaces.Dec()
                    GetEndIf(sb, spaces, TableGen)

                End If

                For Each Column As PCGColumn In TableGen.Columns
                    If Column.DisplayInGrid = True Then
                        sb.AppendLine()
                        sb.AppendLine(spaces.ToString & "TableCell = new TableCell()" & EndStatement(TableGen))
                        Select Case TableGen.CodeType
                            Case CodeTypeEnum.VB2005
                                sb.AppendLine(spaces.ToString & "TableCell.Text = Reader(""" & Column.BoundField & """).ToString()" & EndStatement(TableGen))
                            Case CodeTypeEnum.CS2005
                                sb.AppendLine(spaces.ToString & "TableCell.Text = Reader[""" & Column.BoundField & """].ToString()" & EndStatement(TableGen))
                        End Select

                        sb.AppendLine(spaces.ToString & "TableRow.Cells.Add(TableCell)" & EndStatement(TableGen))
                    End If
                Next

                sb.AppendLine()
                sb.AppendLine(spaces.ToString & "TableCell = new TableCell()" & EndStatement(TableGen))

                sb.Append(DimVariableAsNewClassName(TableGen, spaces, "btn", "HtmlInputButton"))



                Select Case TableGen.CodeType
                    Case CodeTypeEnum.VB2005
                        sb.AppendLine(spaces.ToString & "btn.Attributes.Add(""onclick"", ""GridClick("" & Reader(""" & TableGen.AutoNumberColumn & """).ToString() & "");"")" & EndStatement(TableGen))
                    Case CodeTypeEnum.CS2005
                        sb.AppendLine(spaces.ToString & "btn.Attributes.Add(""onclick"", ""GridClick("" " & OperatorConcatenate(TableGen) & " Reader[""" & TableGen.AutoNumberColumn & """].ToString()" & OperatorConcatenate(TableGen) & " "");"")" & EndStatement(TableGen))
                End Select
                sb.AppendLine(spaces.ToString & "btn.Value = ""Edit""" & EndStatement(TableGen))
                sb.AppendLine(spaces.ToString & "btn.Attributes.Add(""class"", ""GridEditButton"")" & EndStatement(TableGen))
                sb.AppendLine(spaces.ToString & "TableCell.Controls.Add(btn)" & EndStatement(TableGen))

                sb.AppendLine(spaces.ToString & "TableRow.Cells.Add(TableCell)" & EndStatement(TableGen))

                sb.AppendLine()

                sb.AppendLine(spaces.ToString & "grd.Rows.Add(TableRow)" & EndStatement(TableGen))

                spaces.Dec()
                VBLoop(sb, TableGen, spaces)

                ReaderDotClose(sb, TableGen, spaces)
        End Select

        Select Case TableGen.CommentLevel
            Case CommentLevelEnum.Basic

            Case CommentLevelEnum.Verbose
                sb.AppendLine()
                AddComment(sb, TableGen, spaces, "Set's the label just above the grid to the number of rows in the grid.")

        End Select
     
        Select Case TableGen.Paging
            Case PagingEnum.Basic, PagingEnum.HighSpeed
            Case PagingEnum.None
                Select Case TableGen.ViewType
                    Case ViewTypeEnum.GridView, ViewTypeEnum.CodeBehind
                        sb.AppendLine(spaces.ToString & "lblRecordsFound.Text = ""Records Found "" " & OperatorConcatenate(TableGen) & " grd.Rows.Count" & EndStatement(TableGen))
                    Case ViewTypeEnum.Repeater

                        sb.AppendLine(spaces.ToString & "lblRecordsFound.Text = ""Records Found "" " & OperatorConcatenate(TableGen) & " grd.Items.Count" & EndStatement(TableGen))

                End Select
        End Select

        ConnectionDotClose(sb, TableGen, spaces)

        Select Case TableGen.Paging
            Case PagingEnum.HighSpeed
                sb.AppendLine()
                sb.AppendLine(spaces.ToString & "cmdFirst.Enabled = " & GetTrue(TableGen) & EndStatement(TableGen))
                sb.AppendLine(spaces.ToString & "cmdPrevious.Enabled = " & GetTrue(TableGen) & EndStatement(TableGen))
                sb.AppendLine(spaces.ToString & "cmdNext.Enabled = " & GetTrue(TableGen) & EndStatement(TableGen))
                sb.AppendLine(spaces.ToString & "cmdLast.Enabled = " & GetTrue(TableGen) & EndStatement(TableGen))
                sb.AppendLine()
                sb.AppendLine(spaces.ToString & IfEquals(ReadSessionField(TableGen.SafeTableName & ".CurrentPage", TableGen), """1""", TableGen))
                spaces.Inc()
                sb.AppendLine(spaces.ToString & "cmdFirst.Enabled = " & GetFalse(TableGen) & EndStatement(TableGen))
                sb.AppendLine(spaces.ToString & "cmdPrevious.Enabled = " & GetFalse(TableGen) & EndStatement(TableGen))
                spaces.Dec()
                GetEndIf(sb, spaces, TableGen)

                Select Case TableGen.CodeType
                    Case CodeTypeEnum.VB2005
                        sb.AppendLine(spaces.ToString & IfEquals(ReadSessionField(TableGen.SafeTableName & ".CurrentPage", TableGen), "GetPageCount()", TableGen))
                    Case CodeTypeEnum.CS2005
                        sb.AppendLine(spaces.ToString & IfEquals(ReadSessionField(TableGen.SafeTableName & ".CurrentPage", TableGen), "GetPageCount().ToString()", TableGen))
                End Select
                spaces.Inc()
                sb.AppendLine(spaces.ToString & "cmdNext.Enabled = " & GetFalse(TableGen) & EndStatement(TableGen))
                sb.AppendLine(spaces.ToString & "cmdLast.Enabled = " & GetFalse(TableGen) & EndStatement(TableGen))
                spaces.Dec()

                GetEndIf(sb, spaces, TableGen)
        End Select

        spaces.Dec()


        Select Case TableGen.CommentLevel
            Case CommentLevelEnum.Basic

            Case CommentLevelEnum.Verbose
                sb.AppendLine()
                AddComment(sb, TableGen, spaces, "End of the function/sub DataBind")

        End Select
     
        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005
                sb.AppendLine(spaces.ToString & "End Sub")
            Case CodeTypeEnum.CS2005
                sb.AppendLine(spaces.ToString & "}")
        End Select

        'LoadSubComboBoxSearchInitializeCodeVB(spaces, sb, TableGen)

        Return sb.ToString

    End Function

    Private Shared Function GetMasterAddNewClick(ByVal TableGen As RawCreateScript) As String
        Dim sb As New StringBuilder
        Dim spaces As New Space
        sb.AppendLine()
        spaces.Inc()

        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005

                Select Case TableGen.CommentLevel
                    Case CommentLevelEnum.Basic
                        AddComment(sb, TableGen, spaces, "cmdAddNew_Click event is run when the user clicks the AddNew button")

                    Case CommentLevelEnum.Verbose
                        AddComment(sb, TableGen, spaces, "The Pageload function is fired before cmdAddNew_Click event is fired.")

                End Select

            Case CodeTypeEnum.CS2005

                Select Case TableGen.CommentLevel
                    Case CommentLevelEnum.Basic
                        AddComment(sb, TableGen, spaces, "cmdAddNew_Click event is run when the user clicks the AddNew button")

                    Case CommentLevelEnum.Verbose
                        AddComment(sb, TableGen, spaces, "The Pageload function is fired before cmdAddNew_Click event is fired.")

                End Select

        End Select

        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005
                sb.AppendLine(spaces.ToString() & "Protected Sub cmdAddNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdAddNew.Click")
            Case CodeTypeEnum.CS2005
                sb.AppendLine(spaces.ToString() & "protected void cmdAddNew_Click(object sender, EventArgs e)")
                sb.AppendLine(spaces.ToString() & "{")
        End Select
        spaces.Inc()
        If TableGen.EditKeyPassing = EditKeyPassingEnum.Session Then

            Select Case TableGen.CommentLevel
                Case CommentLevelEnum.Basic

                Case CommentLevelEnum.Verbose
                    sb.AppendLine()
                    AddComment(sb, TableGen, spaces, "Set the Session variable Edit" & TableGen.TableName & "." & TableGen.AutoNumberColumn & " to a string with no characters")
                    AddComment(sb, TableGen, spaces, "By sending no string we are telling the next page Edit" & TableGen.TableName & ".aspx that we are adding a new record and not editing an existing record")

            End Select

            Select Case TableGen.CodeType
                Case CodeTypeEnum.VB2005
                    sb.AppendLine(spaces.ToString() & "Session(""Edit" & TableGen.TableName & "." & TableGen.AutoNumberColumn & """) = """"")
                Case CodeTypeEnum.CS2005
                    sb.AppendLine(spaces.ToString() & "Session[""Edit" & TableGen.TableName & "." & TableGen.AutoNumberColumn & """] = """";")
            End Select
        Else
            'do nothing
        End If

        Select Case TableGen.CommentLevel
            Case CommentLevelEnum.Basic
                AddComment(sb, TableGen, spaces, "Go to the page : Edit" & TableGen.TableName & ".aspx")

            Case CommentLevelEnum.Verbose
                AddComment(sb, TableGen, spaces, "Response.redirect will stop move to the page specified and not execute any more code after it.")

        End Select

        sb.AppendLine(spaces.ToString() & "Response.Redirect(""Edit" & TableGen.OutputFileName & ".aspx"")" & EndStatement(TableGen))
        spaces.Dec()

        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005

                Select Case TableGen.CommentLevel
                    Case CommentLevelEnum.Basic

                    Case CommentLevelEnum.Verbose
                        AddComment(sb, TableGen, spaces, "End Sub - cmdAddNew_Click")

                End Select

                sb.AppendLine(spaces.ToString() & "End Sub")
                sb.AppendLine("")
            Case CodeTypeEnum.CS2005

                Select Case TableGen.CommentLevel
                    Case CommentLevelEnum.Basic

                    Case CommentLevelEnum.Verbose
                        AddComment(sb, TableGen, spaces, "End function - cmdAddNew_Click")

                End Select
                sb.AppendLine(spaces.ToString() & "}")
        End Select


        Return sb.ToString
    End Function


    Public Function GetMasterSearchClick(ByVal TableGen As RawCreateScript) As String

        Dim sb As New StringBuilder

        If TableGen.IsAnyColumnSearchable = False Then
            Return ""
        End If
        Dim Output As String = ""
        Dim Spaces As New Space(1)
        sb.AppendLine()

        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005
                sb.AppendLine(Spaces.ToString & "Protected Sub cmdSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSearch.Click")
            Case CodeTypeEnum.CS2005
                sb.AppendLine(Spaces.ToString & "protected void cmdSearch_Click(object sender, EventArgs e)")
                sb.AppendLine(Spaces.ToString & "{")
        End Select
        Spaces.Inc()

        If TableGen.Paging = PagingEnum.HighSpeed Then
            sb.AppendLine(Spaces.ToString & SetSessionField(TableGen.SafeTableName & ".CurrentPage", TableGen) & " = 1" & EndStatement(TableGen))

        End If

        sb.AppendLine(Spaces.ToString & "BindGrid()" & EndStatement(TableGen))
        sb.AppendLine()
        If TableGen.Paging = PagingEnum.HighSpeed Then
            sb.AppendLine(Spaces.ToString & "UpdateLabels()" & EndStatement(TableGen))
            sb.AppendLine()
        End If

        Select Case TableGen.EditKeyPassing
            Case EditKeyPassingEnum.Inline
            Case Else
                For Each Column As PCGColumn In TableGen.Columns
                    If Column.Search <> "None" Then
                        If Column.IsDate Then
                            sb.AppendLine(Spaces.ToString & SetSessionField(TableGen.TableName & "." & Column.ControlNameSearch("To"), TableGen) & " = " & Column.ControlNameSearch("To") & "." & Column.ControlAccessorSearch & EndStatement(TableGen))
                            sb.AppendLine(Spaces.ToString & SetSessionField(TableGen.TableName & "." & Column.ControlNameSearch("From"), TableGen) & " = " & Column.ControlNameSearch("From") & "." & Column.ControlAccessorSearch & EndStatement(TableGen))
                        Else
                            sb.AppendLine(Spaces.ToString & SetSessionField(TableGen.TableName & "." & Column.ControlNameSearch, TableGen) & " = " & Column.ControlNameSearch & "." & Column.ControlAccessorSearch & EndStatement(TableGen))
                        End If
                    End If
                Next
        End Select

        Spaces.Dec()

        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005
                sb.AppendLine(Spaces.ToString & "End Sub")
                sb.AppendLine("")
            Case CodeTypeEnum.CS2005
                sb.AppendLine(Spaces.ToString & "}")
        End Select
        Return sb.ToString
    End Function

    Private Sub GetMasterGridOnRowCommand(ByVal sb As StringBuilder, ByVal TableGen As RawCreateScript, ByVal spaces As Space)
        If TableGen.DevelopmentMode = DevelopmentMode.Development Then
            AddComment(sb, TableGen, spaces, "[GetMasterGridOnRowCommand]")
        End If

        'spaces.Inc()

        sb.AppendLine()

        ProtectedSubgrdRowCommandFunctionDeclaration(sb, TableGen, spaces)
        spaces.Inc()

        Select Case TableGen.EditKeyPassing
            Case EditKeyPassingEnum.Inline
                LoadSingleRecordControls(spaces, sb, TableGen, "Dialog")
            Case Else
        End Select
        Select Case TableGen.EditKeyPassing
            Case EditKeyPassingEnum.Inline
            Case Else
                Select Case TableGen.ViewType
                    Case ViewTypeEnum.GridView
                        Select Case TableGen.CodeType
                            Case CodeTypeEnum.VB2005
                                sb.AppendLine(spaces.ToString & "if e.CommandName = """" then")
                                spaces.Inc()

                            Case CodeTypeEnum.CS2005
                                sb.AppendLine(spaces.ToString & "if ( e.CommandName != ""Sort"" ) {")
                                spaces.Inc()
                        End Select
                End Select
        End Select

        Select Case TableGen.ViewType
            Case ViewTypeEnum.GridView
                Select Case TableGen.EditKeyPassing
                    Case EditKeyPassingEnum.Inline
                    Case Else
                        Select Case TableGen.CodeType
                            Case CodeTypeEnum.VB2005
                                sb.AppendLine(spaces.ToString & "Dim " & TableGen.AutoNumberColumn & " As String = grd.Rows(e.CommandArgument).Cells(0).Text")
                            Case CodeTypeEnum.CS2005
                                sb.AppendLine(spaces.ToString & "System.Web.UI.WebControls.GridViewCommandEventArgs EventArgument = (System.Web.UI.WebControls.GridViewCommandEventArgs)e;")
                                sb.AppendLine(spaces.ToString & "int RowIndexOfEditButtonClicked = int.Parse(EventArgument.CommandArgument.ToString());")
                                sb.AppendLine(spaces.ToString & "int " & TableGen.AutoNumberColumn & " = int.Parse(grd.Rows[RowIndexOfEditButtonClicked].Cells[0].Text);")
                        End Select

                        Select Case TableGen.EditKeyPassing
                            Case EditKeyPassingEnum.Session
                                sb.AppendLine(spaces.ToString & SetSessionField("Edit" & TableGen.TableName & "." & TableGen.AutoNumberColumn, TableGen) & " = " & TableGen.AutoNumberColumn & EndStatement(TableGen))
                                sb.AppendLine(spaces.ToString & "Response.Redirect(""Edit" & TableGen.OutputFileName & ".aspx"")" & EndStatement(TableGen))
                            Case EditKeyPassingEnum.Request
                                sb.AppendLine(spaces.ToString & "Response.Redirect(""Edit" & TableGen.OutputFileName & ".aspx?ID="" " & OperatorConcatenate(TableGen) & " " & TableGen.AutoNumberColumn & ")" & EndStatement(TableGen))
                            Case EditKeyPassingEnum.Inline
                                'sb.AppendLine(Spaces.ToString & "Response.Redirect(""Edit" & TableGen.TableName & ".aspx?ID="" & " & TableGen.AutoNumberColumn & ")")
                        End Select
                End Select
            Case Else
                Select Case TableGen.EditKeyPassing
                    Case EditKeyPassingEnum.Session
                        sb.AppendLine(spaces.ToString & SetSessionField("Edit" & TableGen.TableName & "." & TableGen.AutoNumberColumn, TableGen) & " = RowClick.Value" & EndStatement(TableGen))
                        sb.AppendLine(spaces.ToString & "Response.Redirect(""Edit" & TableGen.OutputFileName & ".aspx"")" & EndStatement(TableGen))
                    Case EditKeyPassingEnum.Request
                        sb.AppendLine(spaces.ToString & "Response.Redirect(""Edit" & TableGen.OutputFileName & ".aspx?ID="" " & OperatorConcatenate(TableGen) & " RowClick.Value)" & EndStatement(TableGen))
                    Case EditKeyPassingEnum.Inline
                        'sb.AppendLine(Spaces.ToString & "Response.Redirect(""Edit" & TableGen.TableName & ".aspx?ID="" & RowClick.Value)")
                End Select

        End Select

        If TableGen.DevelopmentMode = DevelopmentMode.Development Then
            AddComment(sb, TableGen, spaces, "[trace1111]")
        End If

        Select Case TableGen.ViewType
            Case ViewTypeEnum.GridView
                Select Case TableGen.EditKeyPassing
                    Case EditKeyPassingEnum.Inline
                    Case Else
                        spaces.Dec()
                        GetEndIf(sb, spaces, TableGen)
                End Select
        End Select
        spaces.Dec()
        GetEndSub(sb, spaces, TableGen)
    End Sub


    Private Sub ProtectedSubgrdRowCommandFunctionDeclaration(ByVal sb As StringBuilder, ByVal Tablegen As RawCreateScript, ByVal Spaces As Space)
        'Non Standard Returns here !

        If Tablegen.DevelopmentMode = DevelopmentMode.Development Then
            AddComment(sb, Tablegen, Spaces, "[ProtectedSubgrdRowCommandFunctionDeclaration]")
        End If

        Select Case Tablegen.EditKeyPassing
            Case EditKeyPassingEnum.Inline
                Select Case Tablegen.ViewType
                    Case ViewTypeEnum.GridView
                        Select Case Tablegen.CodeType
                            Case CodeTypeEnum.VB2005
                                sb.AppendLine(Spaces.ToString & "Protected Sub cmdLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdLoad.Click")

                            Case CodeTypeEnum.CS2005
                                sb.AppendLine(Spaces.ToString & "protected void cmdLoad_Click(object sender, EventArgs e)")
                                sb.AppendLine(Spaces.ToString & "{")
                        End Select
                        '''''''''''Watch out !
                        Exit Sub
                End Select
        End Select

        Select Case Tablegen.ViewType
            Case ViewTypeEnum.GridView
                Select Case Tablegen.CodeType
                    Case CodeTypeEnum.VB2005
                        sb.AppendLine(Spaces.ToString & "Protected Sub grd_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grd.RowCommand")
                    Case CodeTypeEnum.CS2005
                        sb.AppendLine(Spaces.ToString & "protected void grd_OnRowCommand(object sender, GridViewCommandEventArgs e)")
                        sb.AppendLine(Spaces.ToString & "{")

                End Select
            Case Else
                Select Case Tablegen.CodeType
                    Case CodeTypeEnum.VB2005
                        sb.AppendLine(Spaces.ToString & "Protected Sub cmdLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdLoad.Click")

                    Case CodeTypeEnum.CS2005
                        sb.AppendLine(Spaces.ToString & "protected void cmdLoad_Click(object sender, EventArgs e)")
                        sb.AppendLine(Spaces.ToString & "{")
                End Select
        End Select
    End Sub

    Private Sub GetGetSQL(ByVal sb As StringBuilder, ByVal Tablegen As RawCreateScript, ByVal Spaces As Space)
        If Tablegen.DevelopmentMode = DevelopmentMode.Development Then
            AddComment(sb, Tablegen, Spaces, "[GetGetSQL]")
        End If
        sb.AppendLine()
        Select Case Tablegen.CodeType
            Case CodeTypeEnum.VB2005
                sb.AppendLine(Spaces.ToString & "Private Function GetSQL() as String")
            Case CodeTypeEnum.CS2005
                sb.AppendLine(Spaces.ToString & "private string GetSQL()")
                sb.AppendLine(Spaces.ToString & "{")
        End Select
        Spaces.Inc()
        Select Case Tablegen.Paging
            Case PagingEnum.HighSpeed
                '''''''''''''''
                Select Case Tablegen.CodeType
                    Case CodeTypeEnum.VB2005
                        sb.AppendLine(Spaces.ToString & "Dim StartRecord As Long = PageSize * Session(""" & Tablegen.SafeTableName & ".CurrentPage"") - PageSize + 1")
                    Case CodeTypeEnum.CS2005
                        sb.AppendLine(Spaces.ToString & "long StartRecord = PageSize * int.Parse(" & ReadSessionField(Tablegen.SafeTableName & ".CurrentPage", Tablegen) & ") - PageSize + 1" & EndStatement(Tablegen))
                End Select

                Select Case Tablegen.CodeType
                    Case CodeTypeEnum.VB2005
                        sb.AppendLine(Spaces.ToString & "Dim StopRecord As Long = StartRecord + PageSize")
                    Case CodeTypeEnum.CS2005
                        sb.AppendLine(Spaces.ToString & "long StopRecord = StartRecord + PageSize" & EndStatement(Tablegen))
                End Select

                Select Case Tablegen.CodeType
                    Case CodeTypeEnum.VB2005
                        sb.Append(Spaces.ToString & "Dim Columns As String = ""[" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "].*")
                    Case CodeTypeEnum.CS2005
                        sb.Append(Spaces.ToString & "String Columns = ""[" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "].*")
                End Select

                For Each Column As PCGColumn In Tablegen.Columns
                    If Column.DisplayInGrid = True And Column.LinkTableName.Length > 0 And Column.LinkTableKey.Length > 0 And Column.LinkTableValue.Length > 0 Then
                        sb.Append(", " & Column.LinkTableName & Column.ColumnName & "." & Column.LinkTableValue & " as " & Column.LinkTableName & Column.ColumnName & Column.LinkTableValue)
                    End If
                Next
                sb.AppendLine("""" & EndStatement(Tablegen))

                sb.Append(DimVariableAsClassName(Tablegen, Spaces, "Table", "String", """"))
                'sb.Append(Spaces.ToString & "Dim Table As String = """)
                Dim counter As Long = 0
                For Each Column As PCGColumn In Tablegen.Columns
                    If Column.DisplayInGrid = True And Column.LinkTableName.Length > 0 And Column.LinkTableKey.Length > 0 And Column.LinkTableValue.Length > 0 Then
                        counter = counter + 1
                        If counter > 1 Then
                            sb.Append("(")
                        End If
                    End If
                Next

                sb.Append("[" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "]")

                counter = 0
                For Each Column As PCGColumn In Tablegen.Columns
                    If Column.DisplayInGrid = True And Column.LinkTableName.Length > 0 And Column.LinkTableKey.Length > 0 And Column.LinkTableValue.Length > 0 Then
                        If counter > 0 Then
                            sb.Append(") ")
                        End If
                        counter = counter + 1
                        sb.Append(" left join [" & Tablegen.SchemaName & "].[" & Column.LinkTableName & "] " & Column.LinkTableAlias & " on [" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "]." & Column.ColumnName & " = " & Column.LinkTableName & Column.ColumnName & "." & Column.LinkTableKey & " ")
                    End If
                Next

                sb.AppendLine("""" & EndStatement(Tablegen))
                Select Case Tablegen.CodeType
                    Case CodeTypeEnum.VB2005
                        sb.AppendLine(Spaces.ToString & "Dim WhereClause As String = """"")
                    Case CodeTypeEnum.CS2005
                        sb.AppendLine(Spaces.ToString & "String WhereClause = """";")
                End Select

                For Each Column As PCGColumn In Tablegen.Columns
                    If Column.Search <> "None" Then
                        Select Case Tablegen.CodeType
                            Case CodeTypeEnum.VB2005, CodeTypeEnum.CS2005
                                Select Case Column.DataType.ToUpper
                                    Case "BIT"
                                        sb.AppendLine(Spaces.ToString & IfEquals(Column.ControlNameSearch & "." & Column.ControlAccessorSearch, """Checked""", Tablegen))
                                        Spaces.Inc()
                                        'If Tablegen.GxxroupColumnName = "" Then
                                        sb.AppendLine(Spaces.ToString & "WhereClause = WhereClause " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "].[" & Column.ColumnName & "] <> 0 """ & EndStatement(Tablegen))
                                        'Else
                                        '    sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.TableName & "].[" & Column.ColumnName & "] <> 0 """ & EndStatement(Tablegen))
                                        'End If

                                        Spaces.Dec()
                                        sb.AppendLine(Spaces.ToString & GetElseIfEquals(Column.ControlNameSearch & "." & Column.ControlAccessorSearch, """Unchecked""", Tablegen))
                                        Spaces.Inc()
                                        'If Tablegen.GxroupColumnName = "" Then
                                        sb.AppendLine(Spaces.ToString & "WhereClause = WhereClause " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "].[" & Column.ColumnName & "] = 0 """ & EndStatement(Tablegen))
                                        'Else
                                        '    sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.TableName & "].[" & Column.ColumnName & "] = 0 """ & EndStatement(Tablegen))
                                        'End If

                                        Spaces.Dec()
                                        GetEndIf(sb, Spaces, Tablegen)
                                        sb.AppendLine(Spaces.ToString & "")
                                    Case "DATETIME", "SMALLDATETIME", "TIMESTAMP"
                                        sb.AppendLine(Spaces.ToString & IfGreaterThan(Column.ControlNameSearch("From") & "." & Column.ControlAccessorSearch & ".Length", "0", Tablegen))
                                        Spaces.Inc()

                                        'If Tablegen.GxroupColumnName = "" Then
                                        sb.AppendLine(Spaces.ToString & "WhereClause = WhereClause " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "].[" & Column.ColumnName & "] >= @" & Column.SafeParameterName("From") & """" & EndStatement(Tablegen))
                                        'Else
                                        '    sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.TableName & "].[" & Column.ColumnName & "] >= @" & Column.SafeParameterName("From") & """" & EndStatement(Tablegen))
                                        'End If

                                        Spaces.Dec()
                                        GetEndIf(sb, Spaces, Tablegen)

                                        sb.AppendLine(Spaces.ToString & IfGreaterThan(Column.ControlNameSearch("To") & "." & Column.ControlAccessorSearch & ".Length", "0", Tablegen))
                                        Spaces.Inc()

                                        'If Tablegen.GxroupColumnName = "" Then
                                        sb.AppendLine(Spaces.ToString & "WhereClause = WhereClause " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "].[" & Column.ColumnName & "] <= @" & Column.SafeParameterName("To") & """" & EndStatement(Tablegen))
                                        'Else
                                        '    sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.TableName & "].[" & Column.ColumnName & "] <= @" & Column.SafeParameterName("To") & """" & EndStatement(Tablegen))
                                        'End If

                                        Spaces.Dec()
                                        GetEndIf(sb, Spaces, Tablegen)
                                    Case Else

                                        'sb.AppendLine( Spaces.ToString & "if " & Column.ControlNameSearch & "." & Column.ControlAccessorSearch & ".Length > 0 Then" )
                                        'if (cboSchoolID.SelectedValue.Length > 0) {

                                        sb.AppendLine(Spaces.ToString & IfGreaterThan(Column.ControlNameSearch & "." & Column.ControlAccessorSearch & ".Length", "0", Tablegen))


                                        Spaces.Inc()
                                        Select Case Column.Search
                                            Case "Exact"
                                                'If Tablegen.GxroupColumnName = "" Then
                                                sb.AppendLine(Spaces.ToString & "WhereClause = WhereClause " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "].[" & Column.ColumnName & "] = @" & Column.SafeParameterName & """" & EndStatement(Tablegen))
                                                'Else
                                                '    sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.TableName & "].[" & Column.ColumnName & "] = @" & Column.SafeParameterName & """" & EndStatement(Tablegen))
                                                'End If

                                            Case Else
                                                'If Tablegen.GxroupColumnName = "" Then
                                                sb.AppendLine(Spaces.ToString & "WhereClause = WhereClause " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "].[" & Column.ColumnName & "] like @" & Column.SafeParameterName & """" & EndStatement(Tablegen))
                                                'Else
                                                '    sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.TableName & "].[" & Column.ColumnName & "] like @" & Column.SafeParameterName & """" & EndStatement(Tablegen))
                                                'End If
                                        End Select
                                        Spaces.Dec()
                                        GetEndIf(sb, Spaces, Tablegen)
                                        sb.AppendLine(Spaces.ToString & "")

                                End Select
                        End Select
                    End If
                Next
                If Tablegen.IsAnyColumnSearchable Then

                    sb.AppendLine(Spaces.ToString & IfGreaterThan("WhereClause.Length", "0", Tablegen))

                    Spaces.Inc()

                    Select Case Tablegen.CodeType
                        Case CodeTypeEnum.VB2005
                            sb.AppendLine(Spaces.ToString & "WhereClause = Replace(WhereClause, "" and "", """", , 1)" & EndStatement(Tablegen))
                        Case CodeTypeEnum.CS2005
                            sb.AppendLine(Spaces.ToString & "WhereClause = WhereClause.Substring(4, WhereClause.Length - 4)" & EndStatement(Tablegen))

                    End Select
                    Spaces.Dec()
                    GetEndIf(sb, Spaces, Tablegen)

                End If

                Select Case Tablegen.Sorting
                    Case True
                        Select Case Tablegen.CodeType
                            Case CodeTypeEnum.VB2005
                                sb.Append(Spaces.ToString & "Dim SortOrder As String = ")
                            Case CodeTypeEnum.CS2005
                                sb.Append(Spaces.ToString & "String SortOrder = ")
                        End Select

                        sb.Append(ReadSessionField(Tablegen.SafeTableName & ".SortExpression", Tablegen) & " " & OperatorConcatenate(Tablegen) & " "" "" " & OperatorConcatenate(Tablegen) & " " & ReadSessionField(Tablegen.SafeTableName & ".SortDirection", Tablegen))

                        sb.AppendLine(EndStatement(Tablegen))
                End Select
                Select Case Tablegen.CodeType
                    Case CodeTypeEnum.VB2005
                        sb.Append(Spaces.ToString & "Dim sql As String = ""WITH " & Tablegen.SafeTableName & "RecordSet AS ( SELECT ROW_NUMBER() OVER (ORDER BY ")
                    Case CodeTypeEnum.CS2005
                        sb.Append(Spaces.ToString & "String sql = ""WITH " & Tablegen.SafeTableName & "RecordSet AS ( SELECT ROW_NUMBER() OVER (ORDER BY ")
                End Select
                Select Case Tablegen.Sorting
                    Case True
                        sb.Append(""" " & OperatorConcatenate(Tablegen) & " SortOrder " & OperatorConcatenate(Tablegen) & " """)
                    Case False
                        sb.Append("[" & Tablegen.TableName & "]" & "." & Tablegen.AutoNumberColumn & " ASC")
                End Select

                sb.AppendLine(") AS RecordSetID, "" " & OperatorConcatenate(Tablegen) & " Columns " & OperatorConcatenate(Tablegen) & " "" FROM "" " & OperatorConcatenate(Tablegen) & " Table" & EndStatement(Tablegen))
                Select Case Tablegen.CodeType
                    Case CodeTypeEnum.VB2005
                        sb.AppendLine(Spaces.ToString & "If Len(WhereClause) > 0 Then")
                    Case CodeTypeEnum.CS2005
                        sb.AppendLine(Spaces.ToString & "if (WhereClause.Length > 0 ) {")
                End Select

                sb.AppendLine(Spaces.ToString & "    sql = sql " & OperatorConcatenate(Tablegen) & " "" where "" " & OperatorConcatenate(Tablegen) & " WhereClause" & EndStatement(Tablegen))
                GetEndIf(sb, Spaces, Tablegen)
                sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " "" ),""" & EndStatement(Tablegen))
                sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " "" " & Tablegen.SafeTableName & "RecordCount AS ( SELECT * FROM " & Tablegen.SafeTableName & "RecordSet, (SELECT MAX(RecordSetID) AS RecordSetCount FROM " & Tablegen.SafeTableName & "RecordSet) AS RC ) """ & EndStatement(Tablegen))
                sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " ""SELECT * FROM " & Tablegen.SafeTableName & "RecordCount WHERE RecordSetID >= "" " & OperatorConcatenate(Tablegen) & " StartRecord " & OperatorConcatenate(Tablegen) & " "" AND RecordSetID < "" " & OperatorConcatenate(Tablegen) & " StopRecord" & EndStatement(Tablegen))

                '''''''''''''''
            Case Else


                sb.AppendLine(DimVariableAsClassName(Tablegen, Spaces, "sql", "String", """""") & EndStatement(Tablegen))

                sb.Append(Spaces.ToString & "sql = sql " & Support.OperatorConcatenate(Tablegen) & " ""select [" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "].*")
                For Each Column As PCGColumn In Tablegen.Columns
                    If Column.DisplayInGrid = True And Column.LinkTableName.Length > 0 And Column.LinkTableKey.Length > 0 And Column.LinkTableValue.Length > 0 Then
                        sb.Append(", " & Column.LinkTableName & Column.ColumnName & "." & Column.LinkTableValue & " as " & Column.LinkTableName & Column.ColumnName & Column.LinkTableValue)
                    End If
                Next

                sb.Append(" from ")
                Dim counter As Long = 0
                For Each Column As PCGColumn In Tablegen.Columns
                    If Column.DisplayInGrid = True And Column.LinkTableName.Length > 0 And Column.LinkTableKey.Length > 0 And Column.LinkTableValue.Length > 0 Then
                        counter = counter + 1
                        If counter > 1 Then
                            sb.Append("(")
                        End If
                    End If
                Next

                sb.Append("[" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "]")

                counter = 0
                For Each Column As PCGColumn In Tablegen.Columns
                    If Column.DisplayInGrid = True And Column.LinkTableName.Length > 0 And Column.LinkTableKey.Length > 0 And Column.LinkTableValue.Length > 0 Then
                        If counter > 0 Then
                            sb.Append(") ")
                        End If
                        counter = counter + 1
                        sb.Append(" left join [" & Column.LinkTableName & "] " & Column.LinkTableName & Column.ColumnName & " on [" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "]." & Column.ColumnName & " = " & Column.LinkTableName & Column.ColumnName & "." & Column.LinkTableKey & " ")
                    End If
                Next

                sb.Append("""")

                'If Tablegen.GxroupColumnName <> "" Then
                '    sb.Append("where [" & Tablegen.GxroupColumnName & "]=@" & PCGColumn.StaticSafeParameterName(Tablegen.GxroupColumnName) & " "" ")
                'End If

                sb.AppendLine(EndStatement(Tablegen))

                If Tablegen.IsAnyColumnSearchable Then
                    'If Tablegen.GxroupColumnName = "" Then

                    Select Case Tablegen.CommentLevel
                        Case CommentLevelEnum.Basic

                        Case CommentLevelEnum.Verbose
                            sb.AppendLine()
                            AddComment(sb, Tablegen, Spaces, "Create a string variable called WhereClause and initialize it to an Empty String.")
                            AddComment(sb, Tablegen, Spaces, "this will be used to add aditional filters based on search controls placed on the form.")

                    End Select

                    Select Case Tablegen.CodeType
                        Case CodeTypeEnum.VB2005
                            sb.AppendLine(Spaces.ToString & "Dim WhereClause As String = """"")
                        Case CodeTypeEnum.CS2005
                            sb.AppendLine(Spaces.ToString & "String WhereClause = """";")
                    End Select
                    'End If
                End If

                For Each Column As PCGColumn In Tablegen.Columns
                    If Column.Search <> "None" Then
                        Select Case Tablegen.CodeType
                            Case CodeTypeEnum.VB2005, CodeTypeEnum.CS2005
                                Select Case Column.DataType.ToUpper
                                    Case "BIT"
                                        sb.AppendLine(Spaces.ToString & IfEquals(Column.ControlNameSearch & "." & Column.ControlAccessorSearch, """Checked""", Tablegen))
                                        Spaces.Inc()
                                        'If Tablegen.GxroupColumnName = "" Then
                                        sb.AppendLine(Spaces.ToString & "WhereClause = WhereClause " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "].[" & Column.ColumnName & "] <> 0 """ & EndStatement(Tablegen))
                                        'Else
                                        '    sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.TableName & "].[" & Column.ColumnName & "] <> 0 """ & EndStatement(Tablegen))
                                        'End If

                                        Spaces.Dec()
                                        sb.AppendLine(Spaces.ToString & GetElseIfEquals(Column.ControlNameSearch & "." & Column.ControlAccessorSearch, """Unchecked""", Tablegen))
                                        Spaces.Inc()
                                        'If Tablegen.GxroupColumnName = "" Then
                                        sb.AppendLine(Spaces.ToString & "WhereClause = WhereClause " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "].[" & Column.ColumnName & "] = 0 """ & EndStatement(Tablegen))
                                        'Else
                                        '    sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.TableName & "].[" & Column.ColumnName & "] = 0 """ & EndStatement(Tablegen))
                                        'End If

                                        Spaces.Dec()
                                        GetEndIf(sb, Spaces, Tablegen)
                                        sb.AppendLine(Spaces.ToString & "")
                                    Case "DATETIME", "SMALLDATETIME", "TIMESTAMP"
                                        sb.AppendLine(Spaces.ToString & IfGreaterThan(Column.ControlNameSearch("From") & "." & Column.ControlAccessorSearch & ".Length", "0", Tablegen))
                                        Spaces.Inc()

                                        'If Tablegen.GxroupColumnName = "" Then
                                        sb.AppendLine(Spaces.ToString & "WhereClause = WhereClause " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "].[" & Column.ColumnName & "] >= @" & Column.SafeParameterName("From") & """" & EndStatement(Tablegen))
                                        'Else
                                        '    sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.TableName & "].[" & Column.ColumnName & "] >= @" & Column.SafeParameterName("From") & """" & EndStatement(Tablegen))
                                        'End If

                                        Spaces.Dec()
                                        GetEndIf(sb, Spaces, Tablegen)

                                        sb.AppendLine(Spaces.ToString & IfGreaterThan(Column.ControlNameSearch("To") & "." & Column.ControlAccessorSearch & ".Length", "0", Tablegen))
                                        Spaces.Inc()

                                        'If Tablegen.GxroupColumnName = "" Then
                                        sb.AppendLine(Spaces.ToString & "WhereClause = WhereClause " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "].[" & Column.ColumnName & "] <= @" & Column.SafeParameterName("To") & """" & EndStatement(Tablegen))
                                        'Else
                                        '    sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.TableName & "].[" & Column.ColumnName & "] <= @" & Column.SafeParameterName("To") & """" & EndStatement(Tablegen))
                                        'End If

                                        Spaces.Dec()
                                        GetEndIf(sb, Spaces, Tablegen)
                                    Case Else

                                        'sb.AppendLine( Spaces.ToString & "if " & Column.ControlNameSearch & "." & Column.ControlAccessorSearch & ".Length > 0 Then" )
                                        'if (cboSchoolID.SelectedValue.Length > 0) {

                                        sb.AppendLine(Spaces.ToString & IfGreaterThan(Column.ControlNameSearch & "." & Column.ControlAccessorSearch & ".Length", "0", Tablegen))

                                        Spaces.Inc()
                                        Select Case Column.Search
                                            Case "Exact"
                                                'If Tablegen.GxroupColumnName = "" Then
                                                sb.AppendLine(Spaces.ToString & "WhereClause = WhereClause " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "].[" & Column.ColumnName & "] = @" & Column.SafeParameterName & """" & EndStatement(Tablegen))
                                                'Else
                                                '    sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.TableName & "].[" & Column.ColumnName & "] = @" & Column.SafeParameterName & """" & EndStatement(Tablegen))
                                                'End If

                                            Case Else
                                                'If Tablegen.GxroupColumnName = "" Then
                                                sb.AppendLine(Spaces.ToString & "WhereClause = WhereClause " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.SchemaName & "].[" & Tablegen.TableName & "].[" & Column.ColumnName & "] like @" & Column.SafeParameterName & """" & EndStatement(Tablegen))
                                                'Else
                                                '    sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " "" and [" & Tablegen.TableName & "].[" & Column.ColumnName & "] like @" & Column.SafeParameterName & """" & EndStatement(Tablegen))
                                                'End If
                                        End Select
                                        Spaces.Dec()
                                        GetEndIf(sb, Spaces, Tablegen)
                                        sb.AppendLine(Spaces.ToString & "")

                                End Select
                        End Select
                    End If
                Next

                If Tablegen.IsAnyColumnSearchable Then
                    'If Tablegen.GxroupColumnName = "" Then

                    sb.AppendLine(Spaces.ToString & IfGreaterThan("WhereClause.Length", "0", Tablegen))

                    Spaces.Inc()

                    Select Case Tablegen.CodeType
                        Case CodeTypeEnum.VB2005
                            sb.AppendLine(Spaces.ToString & "WhereClause = Replace(WhereClause, "" and "", """", , 1)" & EndStatement(Tablegen))
                        Case CodeTypeEnum.CS2005
                            sb.AppendLine(Spaces.ToString & "WhereClause = WhereClause.Substring(4, WhereClause.Length - 4)" & EndStatement(Tablegen))
                    End Select

                    sb.AppendLine(Spaces.ToString & "sql = sql " & OperatorConcatenate(Tablegen) & " ""where "" " & OperatorConcatenate(Tablegen) & " WhereClause" & EndStatement(Tablegen))

                    Spaces.Dec()
                    GetEndIf(sb, Spaces, Tablegen)

                    sb.AppendLine(Spaces.ToString & "")
                    'End If
                End If

                Select Case Tablegen.ViewType
                    Case ViewTypeEnum.GridView
                        If Tablegen.Sorting Then
                            Select Case Tablegen.CodeType
                                Case CodeTypeEnum.CS2005
                                    sb.AppendLine("        if (SessionValue(""" & Tablegen.SafeTableName & ".SortExpression"").Length > 0 )")
                                    sb.AppendLine("        {")
                                    sb.AppendLine("            sql = sql " & Support.OperatorConcatenate(Tablegen) & " "" order by "" " & Support.OperatorConcatenate(Tablegen) & " SessionValue(""" & Tablegen.SafeTableName & ".SortExpression"") " & Support.OperatorConcatenate(Tablegen) & " "" "" " & Support.OperatorConcatenate(Tablegen) & " SessionValue(""" & Tablegen.SafeTableName & ".SortDirection"")" & EndStatement(Tablegen))
                                Case CodeTypeEnum.VB2005
                                    sb.AppendLine("        If Len(Session(""" & Tablegen.SafeTableName & ".SortExpression"")) > 0 Then")
                                    sb.AppendLine("            sql = sql " & Support.OperatorConcatenate(Tablegen) & " "" order by "" " & Support.OperatorConcatenate(Tablegen) & " Session(""" & Tablegen.SafeTableName & ".SortExpression"") " & Support.OperatorConcatenate(Tablegen) & " "" "" " & Support.OperatorConcatenate(Tablegen) & " Session(""" & Tablegen.SafeTableName & ".SortDirection"")" & EndStatement(Tablegen))
                            End Select
                            GetEndIf(sb, Spaces, Tablegen)
                        End If
                End Select
        End Select

        Select Case Tablegen.CodeType
            Case CodeTypeEnum.CS2005
                sb.AppendLine(Spaces.ToString & "return sql" & EndStatement(Tablegen))
            Case CodeTypeEnum.VB2005
                sb.AppendLine(Spaces.ToString & "Return sql" & EndStatement(Tablegen))
        End Select

        Spaces.Dec()
        sb.AppendLine()

        GetEndFunction(sb, Spaces, Tablegen)

        Select Case Tablegen.EditKeyPassing
            Case EditKeyPassingEnum.Inline

                GetDetailASPXSaveClick(Spaces, sb, Tablegen)
        End Select
    End Sub
End Class
